package com.itshare.video.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itshare.video.common.core.constant.enums.ApiInfo;
import com.itshare.video.common.core.constant.enums.DeleteStatus;
import com.itshare.video.common.dto.QueryInfo;
import com.itshare.video.common.dto.Result;
import com.itshare.video.mapper.AdvertisementMapper;
import com.itshare.video.mapper.custom.AdvertisementMapperCustom;
import com.itshare.video.pojo.bo.AdvertisementDTO;
import com.itshare.video.pojo.bo.AdvertisementTimeChangeBO;
import com.itshare.video.pojo.bo.UpdateAdvertisementBO;
import com.itshare.video.pojo.code.MessageType;
import com.itshare.video.pojo.code.PermitStatus;
import com.itshare.video.pojo.dto.UserTokenDTO;
import com.itshare.video.pojo.po.Advertisement;
import com.itshare.video.pojo.po.UserMessage;
import com.itshare.video.pojo.query.AdvertisementQuery;
import com.itshare.video.pojo.vo.FrontAdvertisementVO;
import com.itshare.video.service.AdvertisementService;
import com.itshare.video.util.MessageUtil;
import com.itshare.video.util.mapping.AdvertisementDTOMapping;
import com.itshare.video.util.mq.UserMessageMq;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lyr
 * @create 2020/11/4 21:13
 */
@Service
public class AdvertisementServiceImpl implements AdvertisementService {
    @Resource
    private AdvertisementMapper advertisementMapper;
    @Resource
    AdvertisementMapperCustom advertisementMapperCustom;


    AdvertisementDTOMapping advertisementDTOMapping = Mappers.getMapper(AdvertisementDTOMapping.class);
    @Resource
    UserMessageMq userMessageMq;


    /**
     * 申请添加一个 广告
     *
     * @param advertisementDTO
     */
    @Override
    public void addAdvertisement(AdvertisementDTO advertisementDTO,UserTokenDTO userTokenDTO) {
        Advertisement advertisement = advertisementDTOMapping.from(advertisementDTO);
        advertisement.withUpdateTime(new Date())
                .withCreateTime(new Date())

                .withCreateBy(userTokenDTO.getId().toString())
                .withUpdateBy(userTokenDTO.getId().toString())
                .withPermitStatus(0)
                .withDeleteStatus(0)
                ;
        if(advertisement.getBeginTime()==null) {
            //广告开始时间
            advertisement.withBeginTime(new Date());
        }
        UserMessage to =MessageUtil.getDefaultMsg();
        to.setToUserId(userTokenDTO.getId());
        to.withFeedbackTitle("广告发布等待审核")
                .withMessageType(MessageType.SYS_MSG.type)
                .withToUserMessage("广告发布等待审核");
        userMessageMq.sentMsg(to);
        advertisementMapper.insertSelective(advertisement);
    }

    /**
     * 保存或者插入一条数据
     *
     * @param advertisement
     */
    @Override
    public void saveOrUpdateAdvertisement(Advertisement advertisement) {
        advertisementMapperCustom.insertOrUpdate(advertisement);
    }

    /**
     * 获取 广告信息
     *
     * @param userTokenDTO
     * @param queryInfo
     * @return
     */
    @Override
    public PageInfo<FrontAdvertisementVO> getAdvertisement(UserTokenDTO userTokenDTO, QueryInfo<AdvertisementQuery> queryInfo) {

        return PageHelper.startPage(queryInfo.getPageParam()).doSelectPageInfo(()->{
           advertisementMapperCustom.listFrontAdvertisementByQuery(queryInfo.getQueryObject());
        });
    }

    /**
     * 逻辑删除
     *
     * @param advertisementId
     */
    @Override
    public void logicDeleteAdvertisement(Integer advertisementId) {

        Advertisement advertisement = new Advertisement();
        advertisement.withAdvertisementId(advertisementId)
                .withDeleteStatus(DeleteStatus.deleted.type)
                .withPermitStatus(PermitStatus.no_permit.type);
        Advertisement where = advertisementMapper.selectByPrimaryKey(advertisementId);
        UserMessage msg = MessageUtil.getDefaultMsg()
                .withToUserId(Integer.valueOf(where.getCreateBy()))
                .withToUserMessage("广告下架")
                .withFeedbackTitle("您的广告已被管理员下架")
                .withMessageType(MessageType.SYS_MSG.type);
        userMessageMq.sentMsg(msg);
        advertisementMapper.updateByPrimaryKeySelective(advertisement);
    }

    @Override
    public Result<?> updateAdvertisementStatus(UpdateAdvertisementBO updateAdvertisementBO) {
        Advertisement  advertisement  = new Advertisement();
        Advertisement where = advertisementMapper.selectByPrimaryKey(updateAdvertisementBO.getAdvertisementId());
        if(where==null) {
            return Result.of(ApiInfo.NO_RESOURCE);
        }
        //todo: 通知用户
        if (updateAdvertisementBO.getPermitStatus()==1) {
            //通过
            UserMessage msg = MessageUtil.getDefaultMsg()
                    .withToUserId(Integer.valueOf(where.getCreateBy()))
                    .withToUserMessage("广告通过")
                    .withFeedbackTitle("广告通过")
                    .withMessageType(MessageType.SYS_MSG.type);
            userMessageMq.sentMsg(msg);
            advertisement.setPermitStatus(PermitStatus.permitted.type);
        }else{
            UserMessage msg = MessageUtil.getDefaultMsg()
                    .withToUserId(Integer.valueOf(where.getCreateBy()))
                    .withToUserMessage("广告审核不通过")
                    .withFeedbackTitle("通告，您的广告没通过审核")
                    .withMessageType(MessageType.SYS_MSG.type);
            userMessageMq.sentMsg(msg);
            //不通过
            advertisement.setPermitStatus(PermitStatus.no_permit.type);
            // advertisement.setDeleteStatus(DeleteStatus.deleted.type);

        }

        advertisement.setAdvertisementId(updateAdvertisementBO.getAdvertisementId());
        advertisementMapper.updateByPrimaryKeySelective(advertisement);
        return Result.of(ApiInfo.OK);
    }

    @Override
    public PageInfo<Advertisement> getAdvertisementAllField(UserTokenDTO userTokenDTO, QueryInfo<AdvertisementQuery> queryInfo) {
        return PageHelper
                .startPage(queryInfo.getPageParam())
                .doSelectPageInfo(() ->{
                   advertisementMapperCustom.listByQuery(queryInfo.getQueryObject()) ;
                });
    }

    /**
     * 更新时间和 修改广告位置
     *
     * @param advertisementTimeChangeBO
     * @return
     */
    @Override
    public Result<?> updateRegionOrTime(AdvertisementTimeChangeBO advertisementTimeChangeBO) {
        Advertisement advertisementPO = advertisementDTOMapping.from(advertisementTimeChangeBO);
        //根据主键更新
        advertisementMapper.updateByPrimaryKeySelective(advertisementPO);
        return Result.success();
    }
}
